<template>
  <div class="app-container">
    <div class="header">
      <router-link :to="{path:'./add'}">
        <el-button type="primary">新建账号</el-button>
      </router-link>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="130">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="账号" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" width="250" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" width="130">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status != 999" :type="scope.row.status | statusFilter">
            {{ statusOptions[scope.row.status] }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="操作" width="160">
        <template slot-scope="{row}">
          <div v-if="row.status != 999">
            <el-button v-if="!row.status" size="mini" type="success" @click="handleModifyStatus(row,1)">
              正常
            </el-button>
            <el-button v-else size="mini" @click="handleModifyStatus(row,0)">
              禁用
            </el-button>
            <!-- <el-button size="mini" type="danger" @click="handleDelete(row,$index)">删除</el-button> -->
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import * as user from '@/api/user'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = ['gray', 'success']
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      statusOptions: ['禁用', '正常']
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      user.list().then(response => {
        this.list = response.data
        this.listLoading = false
      }).catch(e => {
        this.listLoading = false
      })
    },
    handleModifyStatus(row, status) {
      row.status = status
      console.log(row)
      user.update({ id: row.id }).then(() => {
        this.$message({
          message: '操作Success',
          type: 'success'
        })
      }).catch(e => {
        this.listLoading = false
      })
    }
    // handleDelete(row, index) {
    //   this.$confirm('此操作将永久删除该, 是否继续?', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(() => {
    //     // deleteNotice({"id":row.id}).then(() => {
    //     //   this.$message({
    //     //     type: 'success',
    //     //     message: '删除成功!'
    //     //   })
    //     // })
    //     this.list.splice(index, 1)
    //   }).catch(() => {
    //     this.$message({
    //       type: 'info',
    //       message: '已取消删除'
    //     })
    //   })
    // }
  }
}
</script>
<style scoped>
.header{
  margin-bottom: 10px;
}
</style>
